package org.apache.flink.web.domain;

import java.util.*;

public class DynamicSqlResult {
    /**
     * 状态
     */
    private int code = 0;
    /**
     * 提示消息
     */
    private String msg = "操作成功";
    /**
     * 执行的Job列表
     */
    private List<Map<String, Object>> listMap = new ArrayList<>();
    /**
     * 执行的Job列表
     * COLUMN_NAME: "ID"
     * COLUMN_SIZE: 32
     * COL_AS: "ID"
     * COL_NAME: "ID"
     * DECIMAL_DIGITS: 0
     * REMARKS: ""
     * TABLE_CAT: ""
     * TABLE_NAME: ""
     * TABLE_SCHEM: ""
     * TYPE_NAME: "DECIMAL"
     * colDim: 1
     * colType: "decimal(32,0)"
     */
    private List<Map<String, Object>> queryMetaCols = new ArrayList<>();

    public DynamicSqlResult() {
    }

    public DynamicSqlResult(SqlResult sqlResult) {
        this.code = "SUCCESS".equals(sqlResult.getState()) ? 0 : -1;
        this.msg = sqlResult.getMessage();

        SqlResult.JobResult jobResult = null;
        for (SqlResult.JobResult job : sqlResult.getJobs()) {
            if (job.getColumns() != null && job.getColumns().length > 0) {
                if (jobResult != null) {
                    throw new RuntimeException("仅支持单查询");
                }
                jobResult = job;
            }
        }
        if(jobResult != null) {
            SqlResult.ResultColumn[] columns = jobResult.getColumns();
            for (SqlResult.ResultColumn column : columns) {
                boolean bMeasurement = Arrays.asList("DECIMAL", "NUMERIC", "NUMBER", "INT", "BIGINT").contains(column.getTypeRoot().toUpperCase());
                Map<String, Object> metaCol = new HashMap<>();
                metaCol.put("COLUMN_NAME", column.getName());
                metaCol.put("COLUMN_SIZE", column.getPrecision());
                metaCol.put("COL_AS", "");
                metaCol.put("COL_NAME", column.getName());
                metaCol.put("DECIMAL_DIGITS", column.getPrecision());
                metaCol.put("REMARKS", "");
                metaCol.put("TABLE_CAT", "");
                metaCol.put("TABLE_NAME", "");
                metaCol.put("TABLE_SCHEM", "");
                metaCol.put("TYPE_NAME", column.getTypeRoot());
                metaCol.put("colDim", !bMeasurement ? 0 : 1);
                metaCol.put("colType", column.getType().toLowerCase());
                this.queryMetaCols.add(metaCol);
            }
            for (String[] data : jobResult.getDatas()) {
                Map<String, Object> metaCol = new HashMap<>();
                for (int i = 0; i < columns.length; i++) {
                    metaCol.put(columns[i].getName(), data[i]);
                }
                this.listMap.add(metaCol);
            }
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<Map<String, Object>> getListMap() {
        return listMap;
    }

    public void setListMap(List<Map<String, Object>> listMap) {
        this.listMap = listMap;
    }

    public List<Map<String, Object>> getQueryMetaCols() {
        return queryMetaCols;
    }

    public void setQueryMetaCols(List<Map<String, Object>> queryMetaCols) {
        this.queryMetaCols = queryMetaCols;
    }
}
